﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<c:import url="/includes/header.jsp"></c:import>
<script src="${pageContext.request.contextPath}/js/lib/angular.min.js"></script>
<!--主要写的css代码-->
<link href="${pageContext.request.contextPath}/style/css/default.css"
	rel="stylesheet" type="text/css" />
<!--主要写的js代码-->
<script src="${pageContext.request.contextPath}/js/src/default.js"
	type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/src/navtive.js"
	type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/src/timer.js"
	type="text/javascript"></script>
</head>
<body>
	<div class="container" style="background: #f9f9f9;">
		<div class="row">
			<div class="col-sm-2">
				<div>
					<span id="logo" style="margin-left: -15px;"> <img
						src="${pageContext.request.contextPath}/images/admin/logo.png"
						width="220px" height="95px">
					</span>
				</div>
				<div class="sidebar" style="min-height: 800px;">
					<ul class="nav">
						<c:forEach items="${menuList}" var="re" varStatus="status">
							<c:choose>
								<c:when test="${re.url!=null&&re.url!='' }">
									<li><a
										href="${pageContext.request.contextPath}/${re.url }"
										target="mainIframe">${re.name }</a></li>
								</c:when>
								<c:otherwise>
									<li class="has-sub"><a href="javascript:void(0);"><span
											style="font-size: 16px;">${re.name }</span><i
											class="fa fa-caret-right fa-fw pull-right"></i></a>
										<ul class="sub-menu">
											<c:forEach items="${re.sub}" var="sub">
												<li><a
													href="${pageContext.request.contextPath}/${sub.url }"
													target="mainIframe" class="sub"><span>${sub.name }</span><span
														class="badge"
														style="background-color: #15b4a2; display: none">0</span></a></li>
											</c:forEach>
										</ul></li>
								</c:otherwise>
							</c:choose>
						</c:forEach>
					</ul>

				</div>
			</div>
			<div class="col-sm-10" style="background-color: #15B4A2;">
				<div ng-app="ng-app" id="ng-app" ng-controller="ngcontroller">
					<div class="col-sm-4" ng-repeat="(index,s) in data track by index">
						<div class="panel panel-default shop-info"
							data-shopOpen="{{s.shop.isOpen}}">
							<div class="panel-body" style="padding: 2px; height: 100px">
								<div class="col-sm-4">
									<span><img style="width: 80px; height: 80px"
										src='${pageContext.request.contextPath}/images/admin/{{s.logo}}'
										alt="..."></span>
								</div>
								<div class="col-sm-8">
									<p>
										<label>自动接单:</label><select class="btn btn-info auto_order"
											data-id="{{s.id}}" ng-if="s.orderAuto==0||s.orderAuto==2"
											style="padding: 0px">
											<option value="0" selected="selected">关闭</option>
											<option value="1">本地接单</option>
											<option value="3">wifi接单</option>
										</select> <select class="btn btn-info auto_order" data-id="{{s.id}}"
											ng-if="s.orderAuto==1" style="padding: 0px">
											<option value="0">关闭</option>
											<option value="1" selected="selected">本地接单</option>
											<option value="2">wifi接单</option>
										</select> <select class="btn btn-info auto_order" data-id="{{s.id}}"
											ng-if="s.orderAuto==3" style="padding: 0px">
											<option value="0">关闭</option>
											<option value="1">本地接单</option>
											<option value="3" selected="selected">wifi接单</option>
										</select>

									</p>
									<p>
										<label>配送设置:</label>
										<!-- 										 <span ng-if="s.deliverConf.state==0" -->
										<!-- 											class="btn-danger">手工发单</span> <span -->
										<!-- 											ng-if="s.deliverConf.state==1 && s.deliverType==1" -->
										<!-- 											class="btn-info">自动发单</span> <span -->
										<!-- 											ng-if="s.deliverConf.state==1 && s.deliverType==0" -->
										<!-- 											class="btn-info">专送</span> -->
										<select class="btn btn-info auto_deliver"
											data-id="{{s.deliverConf.auserId}}"
											ng-if="s.deliverType==1 && s.deliverConf.state==0"
											style="padding: 0px">
											<option value="0" selected="selected">手工发单</option>
											<option value="1">自动发单</option>
										</select> <select class="btn btn-info auto_deliver"
											data-id="{{s.deliverConf.auserId}}"
											ng-if="s.deliverType==1 && s.deliverConf.state==1"
											style="padding: 0px">
											<option value="0">手工发单</option>
											<option value="1" selected="selected">自动发单</option>
										</select>
									</p>
									<p>
										<label>门店状态:</label><select class="btn btn-info shop_open"
											data-id="{{s.shopId}}" ng-if="s.shop.isOpen!=1"
											data-type="{{s.shop_type}}" style="padding: 0px">
											<option value="0" selected="selected">休息中</option>
											<option value="1">营业中</option>
										</select> <select class="btn btn-info shop_open" data-id="{{s.shopId}}"
											data-type="{{s.shop_type}}" ng-if="s.shop.isOpen==1"
											style="padding: 0px">
											<option value="0">休息中</option>
											<option value="1" selected="selected">营业中</option>
										</select>
									</p>

								</div>
							</div>
						</div>
					</div>
					<div class="col-sm-2">
						<div class="panel panel-default">
							<div class="panel-body" style="padding: 5px; height: 95px">
								<p>
									<label>打印机:</label> <span class="btn-danger" id="printerLink">未连接</span>
								</p>
							</div>
						</div>
					</div>
					<div class="col-sm-2">
						<div class="panel panel-default">
							<div class="panel-body" style="padding: 5px; height: 95px">
								<ul class="nav"
									style="font-size: 16px; font-weight: bold; font: white;">
									<li class="dropdown"><a class="dropdown-toggle"
										data-toggle="dropdown" role="button" aria-expanded="false"><i
											class="fa fa-user fa-fw"></i>&nbsp;${admin.username}&nbsp;<span
											class="caret"></span></a>
										<ul class="dropdown-menu" role="menu" style="">
											<li><a
												href="${pageContext.request.contextPath}/admin/setting/enterPwd.do"
												target="mainIframe"><i class="icon-user"></i>修改密码</a></li>
											<li><a
												data-href="${pageContext.request.contextPath}/admin/exit.do"
												id="outLogin">退出平台</a></li>
										</ul></li>
								</ul>
							</div>
						</div>
					</div>
				</div>
				<div>
					<iframe name="mainIframe" width="100%" height="100%"
						style="min-height: 800px; background-color: #ffffff; margin-top: -15px"
						id="mainIframe" frameborder="0"></iframe>
				</div>
			</div>
		</div>
	</div>
	<div style="display: none">
		<audio controls="controls" id="audio_controls">
			<source
				src="${pageContext.request.contextPath}/raw/new_order_push_sound2.mp3"
				type="audio/mpeg" />
			Your browser does not support the audio element.
		</audio>
	</div>
	<div class="modal fade" id="exceptionModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-body" id="exceptionModalBody">
					<h4 class="text-danger"></h4>
				</div>
				<div class="modal-footer" id="modal-footer">
					<button type="button" id="exceptionModalBtn"
						class="btn btn-primary">确定</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
</body>
</html>
<script type="text/javascript">
	var audio = document.getElementById("audio_controls");
	function audio_play(msg) {
		if (msg != null && typeof (msg.src) != 'undefined') {
			var count = parseInt(msg.count);
			var src = "${pageContext.request.contextPath}/raw/" + msg.src;
			audio.src = src;
			var start = 0;
			audio.addEventListener("ended", function() {
				start++;
				if (start >= count) {
					audio.pause();
				} else {
					audio.play();
				}
			});
			if (count == 0) {
				count = 10;
				audio.play();
			} else {
				audio.play();
			}

			if (msg.type == 'deliver_money_exception') {
				$('#exceptionModal').modal('show');
				$("#exceptionModalBody").find("h4").text('配送平台余额不足请充值');
				$("#exceptionModalBtn").click(function() {
					$('#exceptionModal').modal('hide');
				});
			}
		}
	}

	$("#outLogin").click(function() {
		var url = $(this).attr("data-href");
		window.location.href = url;
		nativeLoginOut();
	});

	var sessionOut = function() {
		$('#exceptionModal').modal('show');
		$("#exceptionModalBody").find("h4").text('用户回话已过期，请重新登录！');
		$("#exceptionModalBtn").click(function() {
			$('#exceptionModal').modal('hide');
			top.window.location.reload();
			nativeLoginOut();
			nativeWriteLog("sessionOut:用户回话已过期，请重新登录");
		});
	}

	//检查本地打印机
	var nativeVaildPrinter = function() {
		var str = nativeGetPrinter();
		var data = JSON.parse(str);
		var isUse = false;
		for ( var i in data) {
			if (data[i].use) {
				isUse = true;
				break;
			}
		}
		if (isUse) {
			$("#printerLink").removeClass("btn-danger");
			$("#printerLink").addClass("btn-info");
			$("#printerLink").text("本地已连接");
		} else {
			$("#printerLink").removeClass("btn-info")
			$("#printerLink").addClass("btn-danger");
			$("#printerLink").text("本地未连接");
		}
		return isUse;
	}
	setTimeout(nativeVaildPrinter, 1000);

	var app = angular.module('ng-app', []);
	app.config(function($sceProvider) {
		$sceProvider.enabled(false);
	});
	var $scopeGlod;
	app
			.controller(
					'ngcontroller',
					function($scope, $http, $timeout, $interval) {
						$scopeGlod = $scope;
						$scopeGlod.data = [];
						var cfg_http = {
							headers : {
								'Content-Type' : 'application/x-www-form-urlencoded'
							},
							transformRequest : function(data) {
								return $.param(data);
							}
						};
						$scopeGlod.getShopInfo = function() {
							var url = "${pageContext.request.contextPath}/admin/shop/info.do";
							$http
									.get(url)
									.then(
											function(response) {
												var data = response.data;
												if (data.code == 0) {
													$scopeGlod.data = data.data
													nativeWriteLog("[shopInfo]"
															+ JSON
																	.stringify(data.data));
													// 													
													// 													sessionStorage
													// 															.setItem(
													// 																	"shopInfo",
													// 																	JSON
													// 																			.stringify(data.data));
												} else {
													console.info("网络不稳定");
													nativeWriteLog("getShopInfo:网络不稳定,运行失败");
												}
											},
											function(response) {
												nativeWriteLog("getShopInfo:网络不稳定,运行失败");
											});
						};
						$scopeGlod.getShopInfo();

						//自动接单设置
						$scopeGlod.orderAutoSet = function() {
							$("#ng-app")
									.find("select.auto_order")
									.change(
											function() {
												var url = "${pageContext.request.contextPath}/admin/shop/order/switch.do";
												var orderAuto = $(this).val();
												var id = $(this)
														.attr("data-id");
												var data = {
													"orderAuto" : orderAuto,
													"id" : id
												};
												$http
														.post(url, data,
																cfg_http)
														.then(
																function(
																		response) {
																	var data = response.data;
																	if (data.code == 0) {
																		bootbox
																				.alert(
																						"操作成功！",
																						function() {
																							$scopeGlod
																									.getShopInfo();
																						})
																	} else {
																		bootbox
																				.alert("操作失败！")
																		nativeWriteLog("orderAutoSet:网络不稳定,运行失败");
																	}
																},
																function(
																		response) {
																	nativeWriteLog("orderAutoSet:网络不稳定,运行失败");
																});
											});
						};
						//自动发单设置
						$scopeGlod.deliverAutoSet = function() {
							$("#ng-app")
									.find("select.auto_deliver")
									.change(
											function() {
												var url = "${pageContext.request.contextPath}/admin/deliver/switch.do";
												var state = $(this).val();
												var id = $(this)
														.attr("data-id");
												var data = {
													"state" : state,
													"auserId" : id
												};
												$http
														.post(url, data,
																cfg_http)
														.then(
																function(
																		response) {
																	var data = response.data;
																	if (data.code == 0) {
																		bootbox
																				.alert(
																						"操作成功！",
																						function() {
																							$scopeGlod
																									.getShopInfo();
																						})
																	} else {
																		bootbox
																				.alert("操作失败！")
																		nativeWriteLog("delverAutoSet:网络不稳定,运行失败");
																	}
																},
																function(
																		response) {
																	nativeWriteLog("delverAutoSet:网络不稳定,运行失败");
																});
											});
						};

						//店铺营业设置
						$scopeGlod.shopOpenSet = function() {
							$("#ng-app")
									.find("select.shop_open")
									.change(
											function() {
												var url = "${pageContext.request.contextPath}/admin/shop/open.do";
												var state = $(this).val();
												var shop_type = $(this).attr(
														"data-type");
												var shopId = $(this).attr(
														"data-id");
												var data = {
													"state" : state,
													"shopId" : shopId,
													"type" : shop_type
												};
												$http
														.post(url, data,
																cfg_http)
														.then(
																function(
																		response) {
																	var data = response.data;
																	if (data.code == 0) {
																		bootbox
																				.alert(
																						"操作成功！",
																						function() {
																							$scopeGlod
																									.getShopInfo();
																						})
																	} else {
																		bootbox
																				.alert("操作失败！")
																		nativeWriteLog("delverAutoSet:网络不稳定,运行失败");
																	}
																},
																function(
																		response) {
																	nativeWriteLog("delverAutoSet:网络不稳定,运行失败");
																});
											});
						};
						//延迟初始化
						$timeout(function() {
							$scopeGlod.orderAutoSet();
							$scopeGlod.deliverAutoSet();
							$scopeGlod.shopOpenSet();
						}, 1000);

						//定时刷新店铺信息
						$interval(function() {
							$scopeGlod.getShopInfo();
						}, 30000);

					});
</script>

